import 'package:flutter/material.dart';
import 'package:flutter_app/dao/CityDataDao.dart';
import 'package:flutter_app/model/city_data.dart';
import 'package:http/http.dart' as http;
import 'dart:convert' as convert;

class TstHttp extends StatefulWidget {
  @override
  _httpNetSate createState() => _httpNetSate();
}

class _httpNetSate extends State<TstHttp> {
  var httpResutlString = '';
  List<ProvinceData> widgets = [];

  @override
  void initState() {
    super.initState();
    // loadData();
    loadDataToJson();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        appBar: AppBar(
          title: Text('HTTP 请求'),
        ),
        body: ListView.builder(
          itemCount: widgets.length,
          itemBuilder: (BuildContext context, int position) {
            return getRow(position);
          },
        ));
  }

  Widget getRow(int i) {
    return Padding(
        padding: EdgeInsets.only(left: 10, right: 10, bottom: 10, top: 10),
        child: Container(
          child: Text("Row ${widgets[i].name}"),
        ));
  }

  loadData() async {
    String dataURL = "http://www.mxnzp.com/api/address/list";
    var response = await http.get(dataURL);
    setState(() {
      widgets = convert.jsonDecode(response.body)['data'];
    });
  }

  loadDataToJson() async {
//    CityDataDao.fetch().then((resutl){
//      setState(() {
//        httpResutlString =convert.jsonEncode(resutl);
//      });
//
//    }).catchError((e){
//
//      httpResutlString=e.toString();
//    });

    // try {
    CityData model = await CityDataDao.fetch();
    setState(() {
      print(model.msg);
      widgets = model.data;
      httpResutlString = convert.jsonEncode(model.code);
    });
//    } catch (e) {
//      httpResutlString = e.toString();
//    }
  }
}
